﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LunwenYun.Portal.WebLogic;
using LunwenYun.Infrastructure.Entities.Enum;
using LunwenYun.Portal.Biz;
using LunwenYun.Infrastructure.Entities.Entity;
using LunwenYun.Tool.Framework;
using LunwenYun.Portal.Web.Models;

namespace LunwenYun.Portal.Web.Controllers
{
    /// <summary>
    /// 投稿人
    /// </summary>
    [ErrorHandle]
    [Compress]
    public class mController : SiteBaseController
    {
        [Auth(UserRole.Member)]
        public ActionResult AuthorCenter(int page = 0)
        {
            List<Article> list = SystemBiz.GetMyManuscriptAndLoadMessage(HttpContext.User.Identity.Name, page, 8);
            ViewBag.Data = new ItemListBox<Article>(list)
                .BuildPage(SystemBiz.GetMyManuscriptForPage(HttpContext.User.Identity.Name), page, 8,
                new PageParameter() { Style="home" });
            ViewBag.OrderCount = SystemBiz.GetMyNewOrderForPage(HttpContext.User.Identity.Name);
            return View();
        }

        [Auth(UserRole.Member)]
        public ActionResult ResetPassword()
        {
            ViewBag.OrderCount = SystemBiz.GetMyNewOrderForPage(HttpContext.User.Identity.Name);
            return View();
        }

        [HttpPost]
        [Auth(UserRole.Member)]
        public ActionResult ResetPassword(string oldPwd, string newPwd)
        {
            if (UserBiz.ChangePassword(HttpContext.User.Identity.Name,
                oldPwd, newPwd) != null)
            {
                ViewBag.Success = true;
                ViewBag.Message = "修改成功";
            }
            else
            {
                ViewBag.Success = false;
                ViewBag.Message = "修改失败，原密码错误";
            } 
            ViewBag.OrderCount = SystemBiz.GetMyNewOrderForPage(HttpContext.User.Identity.Name);
            return View();
        }

        //[Auth(UserRole.Member)]
        //public ActionResult Profile()
        //{
        //    return View();
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">journalId</param>
        /// <returns></returns>
        [Auth(UserRole.Member)]
        public ActionResult Submit(int id=-1)
        {
            if (id != -1)
            {
                var journal = new Periodical().Find(id.ToString());
                ViewBag.Journal = journal;
            }
            return View();  
        }

        [Auth(UserRole.Member)]
        [HttpPost]
        public ActionResult Submit(ArticleSubmitModel model)
        {
            if (SystemBiz.SubmitArticle(
                new Article()
                {
                    UserName = HttpContext.User.Identity.Name,
                    Title = model.Title,
                    IndType = (IndType)model.IndType,
                    SubIndType = (SubIndType)model.SubIndType,
                    Aptitude = ";" + model.Aptitude,
                    Language = (Language)model.Language,
                    Abstract = model.Description,
                    PriceRange = model.Budget,
                    ContactInfo = model.Phone,
                    QQ = model.QQ,
                    IsDelete=false,
                    CreateTime = DateTime.Now,
                    ExpireTime=DateTime.Now.AddDays(14),
                    Status = ArticleStatus.OnShow
                }, model.selectedJournal) != null) {
                    return RedirectToAction("submitresult", new { id=1});
            }
            return RedirectToAction("submitresult", new { id = 0 });
        }
        
        [Auth(UserRole.Member)]
        public ActionResult submitresult(int id)
        {
            ViewBag.Type = id;
            return View(); 
        }

        [Auth(UserRole.Member)]
        public ActionResult periodicallaunch(int id,int agent)
        {
            var periodical = new Periodical().Find(id.ToString());
            var pAgent = new Agent().Find(agent.ToString());
            if (periodical == null || !periodical.IsActive || pAgent == null)
            {
                return Redirect("/j/index/" + id);
            }
            ViewBag.Periodical = periodical;
            ViewBag.Agent = new PaperAgentInfo().Find(pAgent.UserName);
            return View();
        }
        [HttpPost]
        [Auth(UserRole.Member)]
        [ValidateInput(false)]
        public ActionResult periodicallaunch(int id, string agentUsername,string Phone,string QQ, decimal price, string introduction)
        {
            var periodical = new Periodical().Find(id.ToString());
            var pAgent = new Agent().Find(c=>c.UserName == agentUsername);
            if (periodical == null || !periodical.IsActive || pAgent == null)
            {
                return Redirect("/j/index/" + id);
            }
            SystemBiz.PeriodicalLaunch(id, HttpContext.User.Identity.Name, pAgent.UserName, Phone, 
                QQ, price, introduction.Replace("\r\n","<br/>").NoHTMLLabel("script"));
            return RedirectToAction("islaunch");
        }
        public ActionResult islaunch()
        {
            return View();
        }
        [Auth(UserRole.Member)]
        public ActionResult ToLaunch(int id, int messageId)
        {
            var article = new Article().Find(id.ToString());
            var message = new MessageBoard().Find(messageId.ToString());
            if (article == null || message == null || article.UserName.ToLower() != HttpContext.User.Identity.Name.ToLower())
            {
                return RedirectToAction("article", new { id = id });
            }
            ViewBag.Article = article;
            ViewBag.MessageId = messageId;
            return View();
        }


        [HttpPost]
        [ValidateInput(false)]
        [Auth(UserRole.Member)]
        public ActionResult postToLaunch(int id, int messageId, decimal price, string introduction)
        {
            var article = new Article().Find(id.ToString());
            var message = new MessageBoard().Find(messageId.ToString());
            if (article == null || message == null)
            {
                return RedirectToAction("article", new { id = id });
            }
            ViewBag.Article = article;
            ViewBag.Agent = new PaperAgentInfo().Find(message.From);
            SystemBiz.NewTrading(id, message.From, price, introduction.Replace("\r\n", "<br />").NoHTMLLabel("script"));
            return View();
        }

        [Auth(UserRole.Member)]
        public ActionResult myorders(int page = 0)
        {
            ViewBag.OrderCount = SystemBiz.GetMyNewOrderForPage(HttpContext.User.Identity.Name);

            List<Trade> orders = SystemBiz.GetMyAllOrder(HttpContext.User.Identity.Name, page, 10);
            ViewBag.Orders = new ItemListBox<Trade>(orders)
                .BuildPage(SystemBiz.GetMyAllOrderForPage(HttpContext.User.Identity.Name), page, 10,
                new PageParameter() { Style = "home" });
            return View();
        }

        [HttpPost]
        [Auth(UserRole.Member)]
        public ActionResult option(int OptionType, int Id,string Token)
        {
            int result = SystemBiz.OrderOption(OptionType, Id, Token);
            if (result == 1)
            {
                return RedirectToAction("myorders");
            }
            return RedirectToAction("isexpire", new { id = result });
        }
        public ActionResult isexpire(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        [Auth(UserRole.Member)]
        public ActionResult industrycb(int industry)
        {
            var organization = new Organization().Find(industry.ToString());
            ViewBag.Organization = organization;
            return View();
        }
        [Auth(UserRole.Member)]
        [HttpPost]
        public ActionResult industrycb(int orgId, string Phone, string QQ,  string introduction)
        {
            var organization = new Organization().Find(orgId.ToString());
            if (organization == null)
            {
                return Redirect("/j/industry/");
            }
            if (SystemBiz.IndustryLaunch(orgId, HttpContext.User.Identity.Name, Phone, QQ,introduction.Replace("\r\n", "<br/>").NoHTMLLabel("script")) != null)
            {
                return RedirectToAction("industryIsLaunch", new { t = 1 });
            }
            return RedirectToAction("industryIsLaunch", new { t = 0 });
        }
        public ActionResult industryIsLaunch(int t=1)
        {
            return View();
        }
        public ActionResult PayFail(int id)
        {
            ViewBag.Id = id;
            return View();
        }
    }
}
